"""
@name : 011文件操作
@author : qt
@projectname: Study
@time: 2022/3/5
"""
import random

def create_big_file():
    with open('ips.txt', 'x') as fp:
        for i in range(0, 120000):
            random_num = str(random.randint(1, 255))
            fp.write('172.25.254.'+random_num+'\n')


def top10_ips():
    # 定义一个字典，用来保存每个ip出现的次数
    ip_count_dict = {}
    # 读取文件
    with open('ips.txt', 'r') as fp:
        while True:
            buff_ips_lst = fp.readlines(6000)
            if not buff_ips_lst:
                break
            # 统计出每个ip出现的次数
            for ip in buff_ips_lst:
                ip = ip.strip('\n')
                if ip in ip_count_dict:
                    ip_count_dict[ip] += 1
                else:
                    ip_count_dict[ip] = 1
    # 得到前十的次数并去重
    ip_count_lst = list(ip_count_dict.values())
    ip_count_lst.sort(reverse=True)
    ip_top10_count_lst = list(set(ip_count_lst[0:10]))
    print(ip_top10_count_lst)
    # 打印前十的ip
    for tmp1, tmp2 in ip_count_dict.items():
        if tmp2 in ip_top10_count_lst:
            print(tmp1, tmp2)





# create_big_file()
top10_ips()